﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLogPC.Model
{
    [SplitTable(SplitType.Day)]//指定按照时间分表
    [SugarTable("{year}{month}{day}_轴位置")]
    internal class AxisPositionModel: BasePlcDataModel
    {
        
        /// <summary>
        /// 轴名称
        /// </summary>
        public string AxisName { get => Name; set => Name = value; }  

       /// <summary>
       /// 轴位置
       /// </summary>
       public int AxisValue { get=> Value; set => Value = value; } 
        public AxisPositionModel()
        {
        }

        public AxisPositionModel(string address, DateTime samplingTime, int value, string axisName, string? remark = null)
        {
            Address = address;
            SamplingTime = samplingTime;
            Value = value;
            AxisName = axisName;
            Remark = remark;
        }

    }

    [SplitTable(SplitType.Day)]//指定按照时间分表
    [SugarTable("{year}{month}{day}_IO状态")]
    internal class IOModel : BasePlcDataModel
    {
        /// <summary>
        /// IO名称
        /// </summary>
        public string IOName { get => Name; set => Name = value; }
        /// <summary>
        /// IO值
        /// </summary>
        public bool IOValue { get => Value == 1; set => Value = value ? 1 : 0; } //将int值转换为bool值
        public IOModel()
        {
        }
        public IOModel(string address, DateTime samplingTime, int value, string ioName, string? remark = null)
        {
            Address = address;
            SamplingTime = samplingTime;
            Value = value;
            IOName = ioName;
            Remark = remark;
        }
    }

    [SplitTable(SplitType.Day)]//指定按照时间分表
    [SugarTable("{year}{month}{day}_报警记录")]
    internal class AlarmModel: BasePlcDataModel
    {


        /// <summary>
        /// 报警ID
        /// </summary>
        [SugarColumn(ColumnName = "AlarmId")]
        public int AlarmId { get; set; } = 10086;

        /// <summary>
        /// 报警时间
        /// </summary>
        [SugarColumn(ColumnName = "AlarmTime")]
        public DateTime AlarmTime { get; set; }

        /// <summary>
        /// 报警取消时间
        /// </summary>
        [SugarColumn(ColumnName = "AlarmCancelTime")]
        public DateTime AlarmCancelTime { get; set; }

        /// <summary>
        /// 激活状态
        /// </summary>
        [SugarColumn(ColumnName = "Alive")]
        public bool Alive { get; set; }

        /// <summary>
        /// 设备ID
        /// </summary>
        [SugarColumn(ColumnName = "EqpId")]
        public string? EqpId { get; set; } = "1";

        /// <summary>
        /// 报警级别
        /// </summary>
        [SugarColumn(ColumnName = "AlarmLevel")]
        public string AlarmLevel { get; set; }="一般";

        /// <summary>
        /// 描述（英文）
        /// </summary>
        //[SugarColumn(ColumnName = "Description")]
        public string? Description { get; set; } = "";

        /// <summary>
        /// 描述（中文）允许为空
        /// </summary>
        [SugarColumn(ColumnName = "DescriptionCN",IsNullable = true)]

        public string? DescriptionCN { get; set; }

        /// <summary>
        /// PLC地址
        /// </summary>
        //[SugarColumn(ColumnName = "PlcAddr")]
        //public int PlcAddr { get; set; }

        /// <summary>
        /// 报警类型
        /// </summary>
        [SugarColumn(ColumnName = "AlarmType")]
        public string? AlarmType { get; set; } = "重故障";

        /// <summary>
        /// 单元ID
        /// </summary>
        [SugarColumn(ColumnName = "UnitId")]
        public int UnitId { get; set; } = 0;

        /// <summary>
        /// 站ID
        /// </summary>
        [SugarColumn(ColumnName = "StationId")]
        public int? StationId { get; set; } = 0; // 表格中该列数据为空，设为可空类型

        /// <summary>
        /// 通道ID
        /// </summary>
        [SugarColumn(ColumnName = "ChannelId")]
        public int? ChannelId { get; set; } = 0; // 表格中该列数据为空，设为可空类型

    }

    /// <summary>
    /// 能耗数据模型（示例）
    /// </summary>
    [SplitTable(SplitType.Day)]//指定按照时间分表
    [SugarTable("{year}{month}{day}_能耗数据")]
    internal class EnergyDataModel : BasePlcDataModel
    {
        /// <summary>
        /// 能耗名称
        /// </summary>
        public string EnergyName { get => Name; set => Name = value; }

        /// <summary>
        /// 能耗值
        /// </summary>
        public float EnergyValue 
        { 
            get => BitConverter.ToSingle(BitConverter.GetBytes(Value), 0); 
            set => Value = BitConverter.ToInt32(BitConverter.GetBytes(value), 0); 
        }

        public EnergyDataModel()
        {
        }

        public EnergyDataModel(string address, DateTime samplingTime, float value, string energyName, string? remark = null)
        {
            Address = address;
            SamplingTime = samplingTime;
            EnergyValue = value;
            EnergyName = energyName;
            Remark = remark;
        }
    }

    internal class BasePlcDataModel
    {
        [SugarColumn(IsPrimaryKey = true)]
        public long Id { get; set; }

        /// <summary>
        /// 采集时间
        /// </summary>
        [SplitField] //分表以当前这个属性  对应的数据库表字段数据为维度来分表
        public DateTime? SamplingTime { get; set; }

        /// <summary>
        /// 寄存器地址
        /// </summary>
        public string? Address { get; set; } = string.Empty;
        /// <summary>
        /// 备注
        /// </summary>
        public string? Remark { get; set; } = string.Empty;

        /// <summary>
        /// PLC值
        /// </summary>
        protected int Value { get; set; }= 0;

        protected string? Name { get; set; } = string.Empty;

        public BasePlcDataModel()
        {
        }
    }
}
